package com.zib.bilibili.presenter.impl;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.zib.bilibili.base.BasePresenter;
import com.zib.bilibili.bean.TuijianBean;
import com.zib.bilibili.model.TuijianModel;
import com.zib.bilibili.model.impl.TuijianModelImpl;
import com.zib.bilibili.presenter.TuijianPresenter;
import com.zib.bilibili.view.fragment.TuijianFragment;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * ----------BigGod be here!----------/
 * ***┏┓******┏┓*********
 * *┏━┛┻━━━━━━┛┻━━┓*******
 * *┃             ┃*******
 * *┃     ━━━     ┃*******
 * *┃             ┃*******
 * *┃  ━┳┛   ┗┳━  ┃*******
 * *┃             ┃*******
 * *┃     ━┻━     ┃*******
 * *┃             ┃*******
 * *┗━━━┓     ┏━━━┛*******
 * *****┃     ┃神兽保佑*****
 * *****┃     ┃代码无BUG！***
 * *****┃     ┗━━━━━━━━┓*****
 * *****┃              ┣┓****
 * *****┃              ┏┛****
 * *****┗━┓┓┏━━━━┳┓┏━━━┛*****
 * *******┃┫┫****┃┫┫********
 * *******┗┻┛****┗┻┛*********
 * ━━━━━━神龙出没━━━━━━
 * <p/>
 * 项目名称：@string/app_name
 * 类描述：
 * 创建人：jackie
 * 创建时间：2016/7/7
 * 修改人：jackie
 * 修改时间：2016/7/7
 * 修改备注：
 */
public class TuijianPresentImpl extends BasePresenter<TuijianFragment> implements TuijianPresenter {
    private ICallback iCallback;

    public TuijianPresentImpl(ICallback iCallback) {
        this.iCallback = iCallback;
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    iCallback.onSuccess((TuijianBean) msg.obj);
                    break;
                case -1:
                    iCallback.onfailure();
                    break;
            }
        }
    };

    private TuijianModel tuijianModel;
    @Override
    public void getTuijianBean() {
        tuijianModel=new TuijianModelImpl();
        tuijianModel.getTuijianBean(new Callback<TuijianBean>() {
            @Override
            public void onResponse(Call<TuijianBean> call, final Response<TuijianBean> response) {
                if (response.isSuccessful()){
                    final TuijianBean tuijianBean=response.body();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Message msg = handler.obtainMessage();
                            msg.what = 1;
                            msg.obj = tuijianBean;
                            handler.sendMessage(msg);
                        }
                    });

                }
            }

            @Override
            public void onFailure(Call<TuijianBean> call, Throwable t) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = handler.obtainMessage();
                        msg.what = -1;
                        handler.sendMessage(msg);
                    }
                });

            }
        });

    }

    public interface  ICallback{
        void onSuccess(TuijianBean obj);
        void onfailure();
    }

}
